package com.arnold.learn.job;

import com.arnold.learn.couchbase.CouchbaseUtil;
import com.arnold.learn.impala.ImpalaUtil;
import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.CouchbaseCluster;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;

/**
 * Created by arnold.zhu on 2017/7/27.
 */
public class InvokeStatJob implements Job {

    private Logger logger = LoggerFactory.getLogger(InvokeStatJob.class);

    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        // Create a cluster reference(only one)
        CouchbaseCluster cluster = CouchbaseUtil.getCluster();

        Bucket bucket = CouchbaseUtil.openBucket(cluster, "cb_date_transaction");

        logger.info("开始定时任务...");

        Connection connection = ImpalaUtil.createConnection();

        InvokeStatJobDetail.addToday_number(connection, bucket);
        InvokeStatJobDetail.addToday_amount(connection, bucket);
        InvokeStatJobDetail.addDay_sales_trends(connection, bucket);

        InvokeStatJobDetail.addLatest_30_day_sales_trends(connection, bucket);
        InvokeStatJobDetail.addTop_10_error_shops(connection, bucket);
        InvokeStatJobDetail.addTop_10_number_of_exceptions(connection, bucket);

        InvokeStatJobDetail.addToday_payment_method(connection, bucket);
        InvokeStatJobDetail.addToday_area(connection, bucket);

        ImpalaUtil.closeConnection(connection);

        CouchbaseUtil.closeBucket(bucket);

        logger.info("定时任务执行完毕...");
    }
}
